package com.minelx.statementsplus.cha.common.csv;

import java.util.ArrayList;
import java.util.List;

import static java.util.Arrays.stream;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

public class Csv {
	private final List<String[]> table;

	private Csv(List<String[]> table) {
		this.table = table;
	}

	public Csv concat(Csv another) {
		List<String[]> result = new ArrayList<>(table);
		result.addAll(another.content());
		return new Csv(result);
	}

	public String toText() {
		return table.stream()
				.map(array -> String.join(",", array))
				.collect(joining("\n"));
	}

	private List<String[]> content() {
		return this.table.subList(1, this.table.size());
	}

	public static Csv text(String csvAsText) {
		return new Csv(stream(csvAsText.split("\n"))
				.map(line -> line.split(","))
				.collect(toList()));
	}
}
